package com.aotain.oframework.util.bean;

import java.beans.FeatureDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
 *  反射的Utils函数集合.
 *  提供侵犯隐私的直接读取filed
 * @author Jacob.cw.Zhang@gmail.com
 */
public class BeanUtil {

	private static Logger logger = LoggerFactory.getLogger(BeanUtil.class);
	
	private BeanUtil() {
	}
	
	/**
	 * Read All Properties,无视private/protected修饰, 不经过getter函数.
	 * @param object
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Object getFieldValue(Object object, String fieldName) throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}
		Object result = null;
		try {
			result = field.get(object);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常{}", e.getMessage());
		}
		return result;
	}
	
	/**
	 * Set All Properties,无视private/protected修饰笄,不经过setter函数.
	 * @param object
	 * @param fieldName
	 * @param value
	 * @throws NoSuchFieldException
	 */
	public static void setFieldValue(Object object, String fieldName, Object value) throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}
		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常:{}", e.getMessage());
		}
	}

	/**
	 * 循环向上转型,获取对象的DeclaredField.
	 * @param object
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Field getDeclaredField(Object object, String fieldName) throws NoSuchFieldException {
		Assert.notNull(object);
		return getDeclaredField(object.getClass(), fieldName);
	}
	
	/**
	 * 循环向上转型,获取类的DeclaredField.
	 * @param clazz
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
		Assert.notNull(clazz);
		Assert.hasText(fieldName);
		for (Class<?> superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		throw new NoSuchFieldException("No such field: " + clazz.getName() + '.' + fieldName);
	}
	
	/**
	 * 判断两个Object的基本类型属性是否相等
	 * @param oldObject
	 * @param newObject
	 * @param c
	 * @return
	 */
	public static boolean isAllBasicFieldsEqual(Object oldObject, Object newObject, Class<?> c) throws NoSuchFieldException {
		Field[] fields = c.getDeclaredFields();
		Object[] args = null;
		if ((oldObject != null && newObject == null) || (oldObject == null && newObject != null)) {
			return false;
		} else if (oldObject != null && newObject != null) {
			for (Field f : fields) {
				try {
					String str = f.getName();
					String fieldName = str.substring(0, 1).toUpperCase().concat(str.substring(1, str.length()));
					Object oldObjectTemp = new Object();
					Object newObjectTemp = new Object();
					if (f.getType().equals(String.class) || f.getType().equals(Integer.class) || f.getType().equals(Long.class)) {
						Class<?> cls = null;
						Method getMethod = c.getMethod("get" + fieldName, cls);
						oldObjectTemp = getMethod.invoke(oldObject, args);
						newObjectTemp = getMethod.invoke(newObject, args);
						if (oldObjectTemp != null && newObjectTemp != null && !oldObjectTemp.equals(newObjectTemp)) {
							return false;
						}
					}
				} catch (Exception e) {
					logger.warn("", e);
				}
			}
		} else if(oldObject == null && newObject == null){
			return true;
		}
		return true;
	}
	
}
